 package xin.petstore.framework.orm.mongodb;
 
 import com.mongodb.WriteResult;
 import com.mongodb.gridfs.GridFSFile;
 import java.io.InputStream;
 import java.util.List;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.query.Criteria;
 import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.data.mongodb.core.query.Update;
 import org.springframework.data.mongodb.gridfs.GridFsTemplate;
 
 public class MongodbService
 {
   private MongoTemplate mongoTemplate;
   private GridFsTemplate gridFsTemplate;
 
   public MongoTemplate getMongoTemplate()
   {
     return this.mongoTemplate;
   }
   public void setMongoTemplate(MongoTemplate mongoTemplate) {
     this.mongoTemplate = mongoTemplate;
   }
   public GridFsTemplate getGridFsTemplate() {
     return this.gridFsTemplate;
   }
   public void setGridFsTemplate(GridFsTemplate gridFsTemplate) {
     this.gridFsTemplate = gridFsTemplate;
   }
 
   public String saveFile(InputStream content, String filename) {
     return this.gridFsTemplate.store(content, filename).getId().toString();
   }
   public String saveFile(InputStream content, String filename, String contentType) {
     return this.gridFsTemplate.store(content, filename, contentType).getId().toString();
   }
   public void saveObject(Object objectToSave) {
     this.mongoTemplate.save(objectToSave);
   }
   public void createCollection(Class<?> entityClass) {
     this.mongoTemplate.createCollection(entityClass);
   }
   public void createCollection(String collectionName) {
     this.mongoTemplate.createCollection(collectionName);
   }
   public boolean isExists(Class<?> entityClass) {
     return this.mongoTemplate.collectionExists(entityClass);
   }
   public boolean isExists(String collectionName) {
     return this.mongoTemplate.collectionExists(collectionName);
   }
   public <T> T findOne(String id, Class<T> entityClass) {
     return this.mongoTemplate.findOne(Query.query(Criteria.where("_id").is(id)), entityClass);
   }
   public <T> List<T> find(Query query, Class<T> entityClass) {
     return this.mongoTemplate.find(query, entityClass);
   }
   public int upsert(Query query, Update update, Class<?> entityClass) {
     return this.mongoTemplate.upsert(query, update, entityClass).getN();
   }
   public int upsert(Query query, Update update, String collectionName) {
     return this.mongoTemplate.upsert(query, update, collectionName).getN();
   }
   public <T> T findAndModify(Query query, Update update, Class<T> entityClass) {
     return this.mongoTemplate.findAndModify(query, update, entityClass);
   }
 }

